//
//  SurprShopDidJieXiaoVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/5.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class SurprShopDidJieXiaoVC: BaseViewController {
    
    var page = -999
    
    var hasmore = "-999"
    
    var dataModels : [DidAnnounModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        groupTableView.register(UINib(nibName: StringFromClass(classType: SurpriseDidJieXiaoCell.self), bundle: nil), forCellReuseIdentifier: SurpriseDidJieXiaoCell.identifer())
        
        groupTableView.contentInset = UIEdgeInsetsMake(ZMNavMaxY + ZMTitlesViewH, 0, 0, 0)
        
        groupTableView.separatorStyle = .none
        
        
        weak var weakSelf = self
        
        groupTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        groupTableView.mj_header.beginRefreshing()
        
        groupTableView.mj_footer = ZMRefreshFooter.init{
            
            
            weakSelf!.loadMoreData()
            
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}




extension  SurprShopDidJieXiaoVC{
    
    
    func loadData() {
        
        page = 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        parmeter["state"] = "3" as AnyObject
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_order&a=one_dollar_order_list", parameters: parmeter) {  [weak self] (responseObject, error) in
            
            
            if error == nil{
                
                self?.hasmore = responseObject!["hasmore"] as! String
                self?.dataModels = [DidAnnounModel]()
                
                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(DidAnnounModel(dic: dic))
                    
                }
                
                
                
                
                self!.groupTableView.mj_footer.resetNoMoreData()
                
                
                if self?.hasmore == "0" {
                    
                    self!.groupTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }
                
                
                if self!.dataModels!.count == 0{
                    
                    
                    self?.showLogoView(text : "暂无相关订单...")
                    
                    
                }
                
                
                self!.groupTableView.mj_header.endRefreshing()
                
                self!.groupTableView.reloadData()
                
                
                
                
                
            }else{
                
                
                
                self!.groupTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
            }
            
            
        }
        
        
        
    }
    
    func loadMoreData() {
        
        page += 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        parmeter["state"] = "3" as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_order&a=one_dollar_order_list", parameters: parmeter) {  [weak self] (responseObject, error) in
            
            
            if error == nil{
                
                self?.hasmore = responseObject!["hasmore"] as! String
                
                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(DidAnnounModel(dic: dic))
                    
                }
                
                
                if self?.hasmore == "0" {
                    
                    self!.groupTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }else{
                    
                    self!.groupTableView.mj_footer.endRefreshing()
                    
                }
                
                
                
                
                self!.groupTableView.reloadData()
                
                
                
                
            }else{
                
                
                
                self!.groupTableView.mj_footer.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
        
    }
    
    
}







// MARK:- tableview代理方法
extension SurprShopDidJieXiaoVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return dataModels!.count
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let tableCell : SurpriseDidJieXiaoCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseDidJieXiaoCell.identifer()) as! SurpriseDidJieXiaoCell
        
        tableCell.model = dataModels?[indexPath.section]
        
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 150
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0.00001
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            
            return 0.00001
            
        } else {
            
            return 8
            
        }
        
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let temp  = SurpriseShopDetailVC()
        
        temp.odId = dataModels![indexPath.section].od_id
        
        navigationController?.pushViewController(temp, animated: true)
        
        
    }
    
    
    
}


